package cn.com.jrr.core.service.power;



import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.power.SysUser;

@Service
public class SysUserService {
	@Resource
	private BaseDaoImpl<SysUser> baseDaoImpl;
	@Autowired
	PageModel <SysUser> pageModel;

	public DataTableResponse<SysUser> list(DataTableRequest dataTableRequest){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="SELECT * FROM sysuser";
		Object [] obj=new String[]{"userId","userName","userEmail","tele","truename","entryid","updateid","entrydt","updatedt"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<SysUser> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,SysUser.class);
		return  dataTableResponse;
	}
	/**
	 * 添加数据
	 * */
	public void add(SysUser sysUser){
		baseDaoImpl.saveO(sysUser);
	}
	/**
	 * 获取一条
	 * */
	public SysUser get(Integer id){
		return  baseDaoImpl.get(SysUser.class, id);
	}
	/**
	 * 删除记录
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from sysuser where userId=?", new Object[]{id});
	}
	/**
	 * 更新记录
	 * */
	public void update(SysUser sysUser){
		baseDaoImpl.update(sysUser);
	}
	public DataTableResponse<SysUser> userListByRoleId(DataTableRequest dataTableRequest,Integer roleId){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="SELECT * FROM sysuser WHERE userId IN (SELECT userId FROM sysuserrole WHERE roleId=?)";
		Object [] obj=new String[]{"userId","userName","userEmail","tele","truename","entryid","updateid","entrydt","updatedt"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<SysUser> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,SysUser.class,new Object[]{roleId});
		return  dataTableResponse;
	}

	/**
	 * 登陆验证
	 */
	public List<SysUser> login(String userName,String password){
		String hql="select * from  SysUser WHERE (userName=? OR userEmail=?) AND password=?";
		List<SysUser> data=baseDaoImpl.findBySql(hql,SysUser.class,userName,userName,password);
		return data;
	}

}
